// pages/index/index.js
const app = getApp()
import {
  pycode
} from '../../utils/pinyin.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    pycode,
    searchWord: '',
    userList: [],
    dataList: [],
    kindObj: [],
    userId: '',
  },
  onPageScroll(event) {
    this.data.scrollTop = event.scrollTop;
  },
  toDetali(e) {
    console.log(e);
    var obj = e.currentTarget.dataset.obj
    wx.navigateTo({
      url: '/packageA/pages/tongXunDetail/tongXunDetail?obj=' + JSON.stringify(obj),
    })

  },

  inputhandle(e) {
    console.log(e);
    var val = e.detail.value
    this.setData({
      searchWord: val
    })

    if (val == '') {
      this.data.dataList.sort((a, b) => {
        return b.initial - a.initial
      })
      this.setData({
        userList: this.data.dataList
      })
    } else {
      var list = this.data.dataList.filter(item => {
        return item.telName.includes(val)
      })
      list.sort((a, b) => {
        return b.initial - a.initial
      })
      this.setData({
        userList: list
      })
      console.log(list);
    }

  },

  selecthandle(e) {
    console.log(e);
  },

  // 获取首字母
  getF(str) {
    return pycode[str.toUpperCase().substr(0, 1)]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userId: app.globalData.userId,
    })
    this.getList();
  },
  getList() {
    var that = this;
    var url = app.globalData.url + "nine/getTongxunlu";
    app.wxRequest(url, {
      userId: that.data.userId,
    }, res => {
      console.log(res);
      if( res.data==null){
        return;
      }

      res.data = res.data.filter(item => {
        return item.telName !== ''
      })
      if (res.flag) {
        that.setData({
          userList: res.data,
          dataList: res.data,
        })
        console.log(res.data);
        that.data.dataList.forEach(ele => {
          ele.initial = that.getF(ele.telName)
        })
        that.data.dataList.sort((a, b) => {
          return b.initial - a.initial
        })
        console.log(that.data.dataList);
        // that.handleData(that.data.dataList)
      }
      wx.stopPullDownRefresh()
    }, err => {

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        // 当前页面的 tabBar 索引
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
  onShareTimeline() { },


})